<?php
namespace newshareTrait\Model;
/**
 * Description of ExtraFunction
 *
 * @author hardy
 */
trait ExtraFunction{
    /********************查询类型************************/
    /**
     * 反射
     * @param $function
     * @return bool
     * @throws \ReflectionException
     */
    public function checkUnusual($function){
        $className = new \ReflectionClass($this);
        return method_exists($className->name, $function);
    }

    /**
     * 获取分页
     * @param $map
     * @param $page
     * @param $page_nums
     * @param string $order
     * @return mixed
     * @throws \ReflectionException
     */
    public function getListForPageData($map, $page, $page_nums, $order = '')
    {
        $data =  $this->getListForPage($map, $page, $page_nums, $order);
        if($this->checkUnusual('setJoinData')){
            foreach ($data as &$item){
                $this->setJoinData($item);
            }
        }
        return $data;
    }

    /**
     * 获取分页
     * @param $map
     * @param $page
     * @param $page_nums
     * @param string $order
     * @return mixed
     * @throws \ReflectionException
     */
    public function getListForPageDataSimple($map, $page, $page_nums, $order = '')
    {
        if($order != ''){
            $this->order($order);
        }
        $data =  $this->where($map)->page($page, $page_nums)->select();
        if($this->checkUnusual('setJoinDataSimple')){
            foreach ($data as &$item){
                $this->setJoinDataSimple($item);
            }
        }elseif ($this->checkUnusual('setJoinData')){
            foreach ($data as &$item){
                $this->setJoinData($item);
            }
        }
        return $data;
    }

    /**
     * 获取一条数据
     * @param $id
     * @return array
     * @throws \ReflectionException
     */
    public function getOneData($id){
        if(empty($id)){
            return [];
        }
        if(is_array($id)){
            $data = $this->where($id)->find();
        }else{
            $data = $this->find($id);
        }
        if(empty($data)){
            return [];
        }
        if ($this->checkUnusual('setJoinData')){
            $this->setJoinData($data);
        }
        return $data;
    }
    /********************删除类型************************/
    /**
     * 条件
     * @param $map
     * @return bool
     */
    function deleteOneData($map){
        if(!($this->where($map)->count())){
            $this->_error = '访问的内容不存在';
            return false;
        }
        $r = $this->del($map['id']);
        if($r === false){
            $this->_error = $this->getError();
        }
        return $r;
    }
    /********************自动完成类型************************/
    public function auto_int_value($value=0){   return 0;}
    public function auto_int_value0($value=0){   return $value;}
    public function auto_int_value1($value=1){   return $value;  }
    public function auto_string($value=''){   return '';}
    /**
     * 自动转化时间
     * $str为空或者为2019-04-04 22:22:22等格式
     * @param $str
     * @return false|int
     */
    function automaticConversionTime($str=''){
        if(empty($str)){
            return time();
        }
        return strtotime($str);
    }
    /********************认证类型************************/
    /**
     * 自动认证时间
     * @param $str
     * @return bool
     */
    function checkPublishDateValue($str) {
        //为空返回true
        if(empty($str)){
            return True;
        }
        //不为空认证格式
        if (preg_match ("/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/s", $str, $parts) || preg_match("/^(\d{4})-(\d{2})-(\d{2}) \d{2}:\d{2}:\d{2}$/s",$str,$parts)){
            //检测是否为日期
            if(checkdate($parts[2],$parts[3],$parts[1])){
                $time = strtotime($str);
                if(0 < $time && $time < 2147483647){
                    return True;
                }else{
                    return false;
                }
            }else{
                return false;
            }
        }else{
            return false;
        }
    }
    /**
     * 认证表关联
     * @param $value   值
     * @param $tableColumn   关联的数据表字段,格式：Table.Column
     * @return mixed
     */
    function checkTableRelevanceExist($value, $tableColumn){
        $arr = explode('.', $tableColumn);
        $table = $arr[0];
        $column = $arr[1];
        if(empty($table) || empty($column) || empty($value)){
            return false;
        }
        return M($table)->where([$column=>$value])->find()? true: false;
    }
    /**
     * 检测DBCount的值是否存在
     * @param $value
     * @param $functionName
     * @return bool
     */
    function checkDBCountValueExist($value, $functionName){
        $check = DBCount::{$functionName}($value);
        return isset($check)? true: false;
    }
}
